home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Demos / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Utilities / String Utilities / WMDataBase Procs < prev   
Encoding:
Text File  |  1994-03-07  |  11.8 KB  |  375 lines  |  [TEXT/IGR0]

  1. | <WMDataBase Procs>, v1.01
  2. | implements linear database with multiple semi-colon separated categories, and one "current" bag
  3. | Each bag contains a list of comma-separated "items"
  4. |
  5. | An item may be a key=value pair.
  6. | The key is expected to be a single word, just like any other Igor name (it is often the name of a global variable, but it need not be).
  7. | The value may be a string with spaces.
  8. | The value string may contain the , ; = chars by use of the TranslateChars() function to
  9. | replace ; with ®  , with ©  and = with ™, etc
  10. | Thus the strings may not originally contain the ®  © or ™ characters or they will be unTranslateCharsd improperly.
  11.  
  12. #include <TranslateChars>
  13.  
  14. | You should not refer to these globals from another procedure window, or you will
  15. | have compilation trouble. Use the GetDataXXX() string functions, below
  16. Function DefaultWMDataBaseGlobals(explanation)
  17.     String explanation
  18.     String/G u_dataBase    |    "bag0:,<paramWave>,<other items for bag0>,;bag1:...."
  19.     String/G u_dbCurrBag,u_dbCurrContents    | "bag" and ",item1,item2,;" for current bag, or ""
  20.     String/G u_dbBadStringChars=",;=:"            | these can't be in a string value (or in a bag, or item, or key, either)
  21.     String/G u_dbReplaceBadChars="©®™Ÿ"        | use these instead.
  22.  
  23.     String prompt="Forget everything in u_dataBase? "+explanation
  24.     DoAlert 1,prompt
  25.     if( V_Flag==1 )
  26.         u_dataBase= ""
  27.         u_dbCurrBag= ""
  28.         u_dbCurrContents= ""
  29.     endif
  30. End
  31.  
  32. | Use these Get and Set routines instead of referring to the global strings
  33. | from another procedure window. These routines were added in v 1.01.
  34. Function/S GetDataBase()
  35.     return u_dataBase
  36. End
  37. Function SetDataBase(s)
  38.     String s
  39.     u_dataBase = s
  40. End
  41.  
  42. Function/S GetDataBag()
  43.     return u_dbCurrBag
  44. End
  45. Function SetDataBag(s)
  46.     String s
  47.     u_dbCurrBag = s
  48. End
  49.  
  50. Function/S GetDataContents()
  51.     return u_dbCurrContents
  52. End
  53. Function SetDataContents(s)
  54.     String s
  55.     u_dbCurrContents = s
  56. End
  57.  
  58. Proc ExamineDataBase(bagName,contents)
  59.     String bagName=u_dbCurrBag
  60.     String contents
  61.     Prompt bagName,"set new bag:",popup,DataBaseListCategories()
  62.     Prompt contents,"contents of "+u_dbCurrBag+" bag:",popup,TranslateChars(u_dbCurrContents,",",";")
  63.  
  64.     DataBaseCurrentBag(bagName)
  65. End
  66.  
  67. | puts the current bag and contents back into into the database,
  68. | and extracts the newBagName as the current bag and contents
  69. | returns length of new bag's contents, 0 if error in creating new bag
  70. | Call with newBagName="" to flush current bag and contents back into the database.
  71. Function DataBaseCurrentBag(newBagName)
  72.     String newBagName
  73.     
  74.     Variable offset
  75.     if( CmpStr(newBagName,u_dbCurrBag)!=0 )
  76.         if( DataBaseNameIsValid(u_dbCurrBag) )    | put current bag back into database
  77.             offset= DataBaseRestoreBag(u_dbCurrBag,u_dbCurrContents)
  78.         endif
  79.         u_dbCurrBag=""
  80.         u_dbCurrContents=""
  81.         if( (DataBaseNameIsValid(newBagName)) %& (DataBaseAddBag(newBagName) > -1) )
  82.             u_dbCurrContents= DataBaseExtractBag(newBagName)
  83.             if( strlen(u_dbCurrContents) > 0 )
  84.                 u_dbCurrBag= newBagName
  85.             endif
  86.         endif
  87.     endif
  88.     return strlen(u_dbCurrContents)
  89. End
  90.  
  91. | Returns truth that there was a current bag to kill
  92. Function DataBaseKillCurrentBag()
  93.     Variable killed
  94.     killed= strlen(u_dbCurrBag)>0
  95.     u_dbCurrBag=""
  96.     u_dbCurrContents=""
  97.     return killed
  98. End
  99.  
  100. | Returns truth there was a bag and (optionally) a key within that bag
  101. | As a side effect, the current bag becomes bagName if it is in the database
  102. Function DataBaseBagAndKeyExist(bagName,key)
  103.     String bagName,key
  104.     Variable exist=0,offset
  105.     if( DataBaseNameIsValid(bagName) )
  106.         if( (CmpStr(bagName,u_dbCurrBag) == 0) %| (DataBaseFindBag(bagName) != -1) )    |  if current bag or bag found in database
  107.             exist=1    | unless key not found
  108.             if( DataBaseNameIsValid(key) )    | check key, too?
  109.                 DataBaseCurrentBag    (bagName)            | SIDE EFFECT: sets current bag
  110.                 if( strsearch(u_dbCurrContents,","+key+"=",0) == -1 )
  111.                     exist= 0    | no key
  112.                 endif
  113.             endif
  114.         endif
  115.     endif
  116.     return exist
  117. End
  118.  
  119. | for instance "Graph0","zeroAngleWhere"
  120. Function/D DataBaseGetBagVariable(bagName,variableKey)
  121.     String bagName,variableKey
  122.     Variable/D value=NaN
  123.     String/G u_str
  124.     if( (DataBaseCurrentBag(bagName) > 0) %& (GetKeyEqualValStr(u_dbCurrContents,variableKey)) )
  125.         value= str2num(u_str)
  126.     else
  127.         Abort "Database error: couldn't find "+variableKey+" for bag "+bagName
  128.     endif
  129.     return value
  130. End
  131.  
  132. | returns truth that variableValue was set
  133. Function DataBaseSetBagVariable(bagName,variableKey,variableValue)
  134.     String bagName,variableKey
  135.     Variable/D variableValue
  136.  
  137.     String valueAsString
  138.     Variable set=0
  139.     if( DataBaseCurrentBag(bagName) > 0 )
  140.         sprintf valueAsString,"%.15g",variableValue
  141.         SetCurrKeyEqualValStr(variableKey,valueAsString)
  142.         set=1
  143.     endif
  144.     return set
  145. End
  146.  
  147. Function/S DataBaseGetBagString(bagName,stringKey)
  148.     String bagName,stringKey
  149.  
  150.     String stringValue=""
  151.     String/g u_str
  152.     if( (DataBaseCurrentBag(bagName) > 0) %& (GetKeyEqualValStr(u_dbCurrContents,stringKey)) )
  153.         stringValue=DataBaseDecodeString(u_str)
  154.     else
  155.         Abort "Database error: couldn't find "+stringKey+" for bag "+bagName
  156.     endif
  157.     return stringValue
  158. End
  159.  
  160. | returns truth that stringValue was set
  161. Function DataBaseSetBagString(bagName,stringKey,stringValue)
  162.     String bagName,stringKey,stringValue
  163.  
  164.     Variable set=0
  165.     if( DataBaseCurrentBag(bagName) > 0 )
  166.         stringValue=DataBaseEncodeString(stringValue)
  167.         SetCurrKeyEqualValStr(stringKey,stringValue)
  168.         set=1
  169.     endif
  170.     return set
  171. End
  172.  
  173. | u_dbCurrContents is assumed to contain comma-separated key=value pairs (the list might be terminated with ";", too).
  174. | Returns offset to where key=value was inserted into u_dbCurrContents
  175. Function SetCurrKeyEqualValStr(key,valueStr)
  176.     String key,valueStr
  177.     Variable en,offset,len
  178.     key= "," + key + "="
  179.     offset= strsearch(u_dbCurrContents,key,0)
  180.     if( offset >= 0 ) | found key, locate value end (up to next ",")
  181.         len= strlen(key)    | len includes , and =
  182.         en= strsearch(u_dbCurrContents,",",offset+len)
  183.         if( en == -1 )
  184.             en= strsearch(u_dbCurrContents,";",offset+len)
  185.             if(en == -1 )
  186.                 en= strlen(u_dbCurrContents)
  187.                 u_dbCurrContents+= ";"        | list should be terminated
  188.             endif
  189.         endif
  190.         u_dbCurrContents[offset,en-1]= key+valueStr
  191.     else
  192.         if( CmpStr(u_dbCurrContents[0,0],",") != 0 )
  193.             u_dbCurrContents[0]=","
  194.         endif
  195.         u_dbCurrContents[0]= key+valueStr
  196.         offset=0
  197.     endif
  198.     return offset
  199. End
  200.  
  201. | returns true if name doesn't contain any invalid chars from u_dbBadStringChars
  202. Function DataBaseNameIsValid(name)
  203.     String name
  204.     Variable nameIsValid
  205.     nameIsValid= (strlen(name) > 0) %& (CmpStr(name,DataBaseEncodeString(name))==0)
  206.     return nameIsValid
  207. End
  208.  
  209. Function/S DataBaseEncodeString(str)
  210.     String str
  211.     str= TranslateChars(str,u_dbReplaceBadChars,"")        | delete any database-compatible chars
  212.     return    TranslateChars(str,u_dbBadStringChars,u_dbReplaceBadChars)
  213. End
  214.  
  215. Function/S DataBaseDecodeString(str)
  216.     String str
  217.     return    TranslateChars(str,u_dbReplaceBadChars,u_dbBadStringChars)
  218. End
  219.  
  220. |returns a semi-colon separated list of categories, suitable for a popup list
  221. Function/S DataBaseListCategories()
  222.     String/G u_str
  223.     String list=u_dbCurrBag+";"
  224.     Variable offset=0
  225.     do        | u_dataBase format is: ";bagA:,item1,;bagB:,item1,item2,;"
  226.         offset= DataBaseNextBag(offset)
  227.         if( offset == -1 )
  228.             break
  229.         endif        
  230.         list += u_str+";"
  231.     while( 1 )
  232.     return list
  233. End
  234.  
  235. | NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE
  236. |
  237. | The private functions below are intended only to support the public functions above.
  238. |
  239. | NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE  NOTE
  240.  
  241. | returns offset for next search, startOffset should initially be 0
  242. | the bag is in the global string u_str
  243. Function DataBaseNextBag(startOffset)
  244.     Variable startOffset
  245.     String/G u_str=""
  246.     Variable en
  247.     | u_dataBase format is: ";bagA:,item1,;bagB:,item1,item2,;"
  248.     startOffset= strsearch(u_dataBase,";",startOffset)    | start of bag
  249.     if( startOffset != -1 )    |  found
  250.         en= strsearch(u_dataBase,":",startOffset)    | end of bag
  251.         if( en != -1 )    |  found
  252.             u_str= u_dataBase[startOffset+1,en-1]
  253.         endif
  254.         startOffset= en
  255.     endif
  256.     return startOffset
  257. End
  258.  
  259. | Locates a bag with given name as "<bagName>:".
  260. | If bagName found, returns char position into global u_dataBase just after ":", or -1.
  261. | that is the char position where the first variableKey or stringKey starts.
  262. |
  263. | u_dataBase format is: ";bagA:,item1,;bagB:,item1,item2,;"
  264. | where item is usually "key=value", though it need not be.
  265. Function DataBaseFindBag(bagName)
  266.     String bagName
  267.  
  268.     String/G u_dataBase
  269.     Variable offset= -1,nameLen
  270.     nameLen= strlen(bagName)     | doesn't include leading ";", or trailing ":,"
  271.     if( DataBaseNameIsValid(bagName) )
  272.         bagName= ";" + bagName + ":,"
  273.         offset= strsearch(u_dataBase,bagName,0)
  274.         if( offset != -1 )    |  found
  275.             | adjust offset to point at the "," after the bag's trailing ":"
  276.             offset += nameLen + 2    | which is  a good place to search for ",item,"
  277.         endif
  278.     endif
  279.     return offset
  280. End
  281.  
  282. | Creates or locates a bag with given name as "<bagName>:".
  283. | If bagName found or added successfully, returns char position into global u_dataBase just after ":", or -1.
  284. | that is the char position where the first variableKey or stringKey starts.
  285. |
  286. | u_dataBase format is: ";bagA:,item1,;bagB:,item1,item2,;"
  287. | where item is usually "key=value", though it need not be.
  288. Function DataBaseAddBag(bagName)
  289.     String bagName
  290.  
  291.     String/G u_dataBase
  292.     Variable offset= -1,nameLen
  293.     nameLen= strlen(bagName)     | doesn't include leading ";", or trailing ":,"
  294.     if( DataBaseNameIsValid(bagName) )
  295.         if( strlen(u_dataBase) < 5 )    | shortest valid list is ";a:,;"
  296.             u_dataBase= ";"    | so that bag list always ends with ";"
  297.         endif
  298.         offset= DataBaseFindBag(bagName)
  299.         if( offset == -1 )    | not found, insert it at front (where most recently added categories will be found quickest)
  300.             u_dataBase[0]=  ";" + bagName + ":,"    | on first call, will be ";bagName:,;"
  301.             offset=  nameLen + 2    | adjust offset to point at the leading "," after the bag's trailing ":", which is a good place to search for ",key="
  302.         endif
  303.     endif
  304.     return offset
  305. End
  306.  
  307. | returns entire bag contents ",item1,item2,;", and removes it from database.
  308. | the bag name and following colon are NOT returned.
  309. | returns "" if bag not found.
  310. Function/S DataBaseExtractBag(bagName)
  311.     String bagName
  312.     
  313.     Variable offset,nextBag
  314.     String bagContents=""
  315.     offset= DataBaseFindBag(bagName)
  316.     if( offset != -1 )    | found bag, extract it
  317.         nextBag= strsearch(u_dataBase,";",offset)    | points at ";" after bagName
  318.         if( nextBag == -1 )
  319.             nextBag=strlen(u_dataBase)-1
  320.         endif
  321.         bagContents=u_dataBase[offset,nextBag]    | doesn't include bag name and colon
  322.         offset-=  strlen(bagName)+1                    | points at first char of bagName
  323.         u_dataBase[offset,nextBag]= ""                    | remove bag and trailing ";" or till end    
  324.     endif
  325.     return bagContents
  326. End
  327.  
  328. | returns offset where bag contents were restored, or -1 
  329. Function DataBaseRestoreBag(bagName,bagContents)
  330.     String bagName,bagContents
  331.     
  332.     Variable offset,nextBag,lastCharOffset
  333.     offset= DataBaseAddBag(bagName)
  334.     if( offset != -1 )    | found bag, offset points at content start (usually leading ",")
  335.         nextBag= strsearch(u_dataBase,";",offset)    | points at ";" after bagName
  336.         if( nextBag == -1 )
  337.             nextBag=strlen(u_dataBase)-1
  338.         endif
  339.         lastCharOffset=strlen(bagContents)-1
  340.         if( CmpStr(bagContents[lastCharOffset,lastCharOffset],";") != 0 )
  341.             bagContents+=";"                                | terminate content properly
  342.         endif
  343.         u_dataBase[offset,nextBag]= bagContents        | replace contents    
  344.     endif
  345.     return offset
  346. End
  347.  
  348. | str is assumed to contain comma-separated key=value pairs (the list might be terminated with ";", too).
  349. | Return value is truth that the key was found
  350. | Returns the string value string given the corresponding key in u_str
  351. Function GetKeyEqualValStr(str,key)
  352.     String str,key
  353.     String/G u_str=""
  354.     Variable st,en,found=0
  355.     if( CmpStr(str[0,0],",") != 0 )
  356.         str[0]=","
  357.     endif
  358.     st= strsearch(str,","+key+"=",0)
  359.     if( st >= 0 ) | found key, get value (up to next "," or ";")
  360.         found= 1
  361.         st += strlen(key)+2    | points at first char after "="
  362.         en= strsearch(str,",",st)
  363.         if( en== -1 )            | no terminating , try ;
  364.             en= strsearch(str,";",st)
  365.             if( en== -1 )
  366.                 en= strlen(str)    | to allow absence of terminating ;
  367.             endif
  368.         endif
  369.         u_str= str[st,en-1]
  370.     endif
  371.     return found
  372. End
  373.  
  374.  
  375.